package com.jiutong.meipenggang.ui.activitys;


import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import butterknife.Bind;
import butterknife.OnClick;

public class ChangePwdActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.rl_change_pwd_old)
    RelativeLayout rlChangePwdOld;
    @Bind(R.id.rl_change_pwd_phone_msg)
    RelativeLayout rlChangePwdPhoneMsg;

    private BaseDialog dialog;
    private String phone;
    private UserInfoPresent userInfoPresent;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_change_pwd;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("安全验证");
    }

    @Override
    protected void onResume() {
        super.onResume();
        phone = SpConfig.getInstance().getString(Constants.USERNAME_STRING);
        userInfoPresent = new UserInfoPresent(this, this);
    }

    @OnClick({R.id.rl_common_title_back, R.id.rl_change_pwd_old, R.id.rl_change_pwd_phone_msg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.rl_change_pwd_old:
                openActivity(OldPwdActivity.class);
                break;
            case R.id.rl_change_pwd_phone_msg:
                showPhoneDialog();
                break;
            default:
        }
    }

    private void showPhoneDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder().
                    layResuorce(R.layout.dialog_common).
                    cancelViewClickEvent(R.id.tv_dialog_cancel).
                    okViewClickEvent(R.id.tv_dialog_ok).
                    confirmId(R.id.tv_dialog_confirm).
                    llBottomId(R.id.ll_dialog).
                    contentTextId(R.id.tv_dialog_content).
                    hintTextId(R.id.tv_dialog_hint).
                    setHintText("确认发送验证码到手机").
                    setContentText(NumUilt.phoneFormat(phone)).
                    setCancelText("取消").
                    setOkText("确定").
                    gravityStyle(Gravity.CENTER).
                    animResource(R.style.dialog_animation_fade).
                    dialogWidth(-2).
                    build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    dialog.dismiss();
//                            SpConfig.getInstance().putString(Constants.SESSIONID_STRING, "");
//                            SpConfig.getInstance().putBool(Constants.ISAUTH_BOOLEAN, false);
//                            SpConfig.getInstance().putString(Constants.REALNAME_STRING, "");
                    userInfoPresent.getAuthCodeByFoget();
                    openActivity(VerifyCodeActivity.class);
                    SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_PWD,true).commit();
                }

                @Override
                public void onOk2Click() {

                }
            });
        }

        dialog.show(getSupportFragmentManager(), TAG);
    }

    @Override
    public String getPhoneNum() {
        return SpConfig.getInstance().getString(Constants.USERNAME_STRING);
    }

    @Override
    public String getVerificatCode() {
        return null;
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {
        if (subjects.getError()==-1){
            toastShortShow(subjects.getMsg());
            Bundle bundle = new Bundle();
            bundle.putString("phone",getPhoneNum());
            openActivity(VerifyCodeActivity.class,bundle);
            finish();
        }else{
            toastShortShow(subjects.getMsg());
        }
    }

    @Override
    public void onSuccess(Object object) {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }
}
